﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLDL
{
    class lib
    {
        //tạo đối tượng kết nối database
        public SqlConnection chungthuc()
        {
            return new SqlConnection("Data Source=THANHLUAN-PC\\SQLEXPRESS;Initial Catalog=QuanLyDieuHanhDuLich;Persist Security Info=True;User ID=sa;Password=123456");
        }
        //load dữ liệu vô bảng tạm,
        public DataTable taobang(string sql)
        {
            SqlConnection conn = chungthuc();
            conn.Open();
            SqlDataAdapter taoluoi = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            dt.Clear();
            taoluoi.Fill(dt);
            conn.Close();
            return dt;
        }
        //thực hiện các lệnh thêm, sửa, xóa,tìm kiếm, lựa chọn
        public string truyvan(string sql)
        {
            string kq;
            SqlConnection conn = chungthuc();
            conn.Open();
            SqlCommand commd = new SqlCommand(sql, conn);
            kq = Convert.ToString(commd.ExecuteNonQuery());
            conn.Close();
            return kq;
            /*
            SqlConnection conn = chungthuc();
            conn.Open();
            SqlCommand commd = new SqlCommand(sql, conn);
            commd.ExecuteNonQuery();
            conn.Close();
             */
        }
        public string GetName(string sql)
        {
            string kq;
            SqlConnection conn = chungthuc();
            conn.Open();
            SqlCommand commd = new SqlCommand(sql, conn);
            kq = Convert.ToString(commd.ExecuteScalar());
            conn.Close();
            return kq;
        }

        public string CheckLogin(string name, string pass)
        {
            string quyen = "";
            SqlConnection conn = chungthuc();
            SqlCommand commd = new SqlCommand("select quyenhan from NGUOI_DUNG where idlogin = '" + name + "' and password = '" + pass + "' ", conn);
            conn.Open();
            SqlDataReader dr = commd.ExecuteReader();
            if (dr.Read())
            {
                quyen = dr[0].ToString();
                conn.Close();
                return quyen;
            }
            else
            {
                conn.Close();
                return quyen;
            }

        }
    }
}
